package logic

import (
	"api/internal/svc"
	"api/internal/types"
	"context"
	"github.com/google/uuid"
	"member_srv/member"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddMemberLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddMemberLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddMemberLogic {
	return &AddMemberLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddMemberLogic) AddMember(req *types.AddMemberRequest) (resp *types.Response, err error) {
	_, err = l.svcCtx.MemberSrv.ListMember(l.ctx, &member.ListMemberRequest{Types: req.Types})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  "系统内部错误",
			Data: nil,
		}, nil
	}
	price := req.Pater / 8
	orderSn := uuid.NewString()
	_, err = l.svcCtx.MemberSrv.AddMember(l.ctx, &member.AddMemberRequest{
		Price:   float32(price),
		Types:   req.Types,
		Title:   req.Title,
		OrderSn: orderSn,
	})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  "充值失败",
			Data: nil,
		}, nil
	}

	return &types.Response{
		Code: 200,
		Msg:  "充值成功",
		Data: nil,
	}, nil
}
